﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySQLDriverCS;
using System.Data;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class DataBaseHelper
    {
        private MySQLConnection connection;
        public DataBaseHelper() {
            try
            {
                connection = new MySQLConnection();
                connection.ConnectionString = new MySQLConnectionString("localhost", "dc", "root", "vwintech", 3306).AsString;
            }
            catch (Exception excep)
            {
                MessageBox.Show(excep.ToString());
            }
          
        }
        public DataTableReader query() {
            DataTableReader reader = null;
            try
            {
                connection.Open();
                MySQLDataAdapter adapter = new MySQLDataAdapter("select * from datalog order by createTime desc limit 1 ", connection);
                DataTable table = new DataTable();
                adapter.Fill(table);
                adapter.Dispose();
                connection.Close();
                reader = table.CreateDataReader();
            }
            catch (Exception excep)
            {
                MessageBox.Show(excep.ToString());
            }
            return reader;
        }

        public void save(String text) {
            try
            {
                connection.Open();
                new MySQLInsertCommand(connection, new object[,] { { "data", text }, { "createTime", DateTime.Now } }, "datalog");
                connection.Close();
            }
            catch (Exception excep)
            {
                MessageBox.Show(excep.ToString());
            }
        }

        //清空
        public void deleteAll() {
            try
            {
                connection.Open();
                new MySQLDeleteCommand(connection,"datalog",null,null);
                connection.Close();
            }
            catch (Exception excep)
            {
                MessageBox.Show(excep.ToString());
            }
        }
    }
}
